package com.lj.demo.eduservice.controller;


import com.lj.demo.commonutils.R;
import com.lj.demo.eduservice.entity.EduChapter;
import com.lj.demo.eduservice.entity.vo.ChapterVo;
import com.lj.demo.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.management.relation.RelationTypeNotFoundException;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author liujiang
 * @since 2020-12-03
 */
@Api(description = "课程章节")
@RestController
@RequestMapping("/eduservice/eduChapter")
//@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    @ApiOperation(value = "根据课程id查询章节")
    @GetMapping("listChapterByCourseId/{courseId}")
    public R listChapterByCourseId(
            @PathVariable String courseId){
        List<ChapterVo> chapterVo = eduChapterService.findChapterById(courseId);
        return R.ok().data("items",chapterVo);
    }

    @ApiOperation(value = "根据章节id查询章节")
    @GetMapping("findById/{id}")
    public R selectById(@PathVariable String id){
        EduChapter byId = eduChapterService.getById(id);
        return R.ok().data("items",byId);
    }

    @ApiOperation(value = "添加章节信息" )
    @PostMapping("/addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.save(eduChapter);
        return R.ok();
    }

    @ApiOperation(value = "修改章节信息")
    @PostMapping("/updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.updateById(eduChapter);
        return R.ok();
    }

    @ApiOperation(value = "根据章节id删除信息")
    @DeleteMapping("deleteChapter/{id}")
    public R deleteChapter(@PathVariable String id){
        boolean b = eduChapterService.deleteById(id);
        if (b){
            return R.ok();
        }return R.error();
    }

}

